package org.example.asm.easydatastructure;

import java.util.Scanner;
import java.util.TreeSet;

/**
 * 集合 通过TreeSet集合来实现
 * 1. 插入元素 2 删除元素 3 存在性检查 4 集合大小 5 前驱 6 后继
 */
public class ListTest04 {
    private static final TreeSet<Integer> set = new TreeSet<>();

    public static void insertValue(int x) {
        // TODO: 实现插入逻辑
        set.add(x);
    }

    public static void eraseValue(int x) {
        // TODO: 实现删除逻辑
        set.remove(x);
    }

    public static boolean xInSet(int x) {
        // TODO: 实现存在性检查
        if (set.contains(x)) {
            return true;
        }
        return false;
    }

    public static int sizeOfSet() {
        // TODO: 返回集合大小
        return set.size();
    }

    public static int getPre(int x) {
        // TODO: 实现找前驱
        Integer pre = set.lower(x);
        return pre == null ? -1 : pre;
    }

    public static int getBack(int x) {
        Integer back = set.higher(x);
        return back == null ? -1 : back;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int q = scanner.nextInt();
        while (q-- > 0) {
            int op = scanner.nextInt();
            switch (op) {
                case 1:
                    insertValue(scanner.nextInt());
                    break;
                case 2:
                    eraseValue(scanner.nextInt());
                    break;
                case 3:
                    System.out.println(xInSet(scanner.nextInt()) ? "YES" : "NO");
                    break;
                case 4:
                    System.out.println(sizeOfSet());
                    break;
                case 5:
                    System.out.println(getPre(scanner.nextInt()));
                    break;
                case 6:
                    System.out.println(getBack(scanner.nextInt()));
                    break;
            }
        }
        scanner.close();
    }
}
